package com.yigerende.controller;

import com.yigerende.pojo.MenuVo;
import com.yigerende.pojo.TMenu;
import com.yigerende.pojo.TType;
import com.yigerende.service.MenuService;
import com.yigerende.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private TypeService typeService;

    //前端传递当前页和显示多少条数据（显示菜品）
    @GetMapping("/findAll/{page}/{limit}")
    public MenuVo findAll(@PathVariable("page") int page, @PathVariable("limit") int limit){
        Map map = new HashMap();
        map.put("start",(page-1)*limit);//当前页-1 * 显示的条数（就为开始索引）
        map.put("end",limit);//结束索引结束显示的条数
        MenuVo menuVo = new MenuVo();
        menuVo.setCode(0);
        menuVo.setMsg("");
        menuVo.setCount(menuService.count());
        menuVo.setData(menuService.findAll(map));
        return menuVo;
    }

    //查询所有类型(类型下拉框使用)
    @GetMapping("/findAll")
    public List<TType> findAll(){
        return typeService.findAll();
    }

    //增加菜品(从前端获取的json数据)
    @PostMapping("/save")
    public int save(@RequestBody TMenu menu){
        return menuService.save(menu);
    }

    //通过id查询菜品
    @GetMapping("/findById/{id}")
    public TMenu findById(@PathVariable("id") Integer id){
        return menuService.findById(id);
    }

    //修改菜品(前端传递的修改的菜品)
    @PutMapping("/update")
    public int update(@RequestBody TMenu menu){
        return menuService.update(menu);
    }

    //删除菜品(通过id)
    @DeleteMapping("/deleteById/{id}")
    public int deleteById(@PathVariable("id") Integer id){
        return menuService.deleteById(id);
    }
}
